home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: netcom.com!marnold
- From: marnold@netcom.com (Matt Arnold)
- Subject: Re: How to get at base class fields from member functions?
- Message-ID: <marnoldDoGEGI.G09@netcom.com>
- Organization: NETCOM On-line Communication Services (408 261-4700 guest)
- References: <internews46B00D1FBD@argonet.co.uk> <4ihhkf$2ni@news5.erols.com> <internews46B0766104@argonet.co.uk>
- Date: Mon, 18 Mar 1996 08:05:06 GMT
- Sender: marnold@netcom4.netcom.com
-
- Dave Mullard <dmullard@argonet.co.uk> writes:
-
- >Chris Cobb <ccobb@erols.com> wrote:
- >> I <dmullard@argonet.co.uk> wrote:
- >> >Given three classes A, B and C I want to have multiple As for each B
- >> and
- >> >multiple Bs for each C. To do this I could create the following.
- >> >
- >> >class b1 : public B
- >> >{
- >> >A a1;
- >> >A a2;
- >> >A a3;
- >> >};
-
- >> >class b2 : public B
- >> >{
- >> >A a1;
- >> >A a2;
- >> >A a3;
- >> >A a4;
- >> >A a5;
- >> >};
-
- >> >class c1 : public C
- >> >{
- >> >b1 b1;
- >> >b2 b2;
- >> >};
-
- >> >The question is, how within functions for class B do I access fields
- >> >within class C, and similarly, how within the class A functions do I
- >> >access fields within class B?
-
-
- >> You have encountered the wall of encapsulation. It is considered poor
- >> form to violate this wall without good reason. The need to violate this
- >
- >> wall may indicate poor design. However, C++ is not so rigid as to
- >> disallow this. The way this is done is to have the class whose members
- >> need to be accessed declare the class who wants to access them a friend:
- >
- >> class C
- >> {
- >> friend class B;
- >> // ...
- >> };
- >
- >> class B
- >> {
- >> friend class A;
- >> // ...
- >> };
- >
- >I obviously have not explained myself very well.
-
- >Maybe instead of *access* I should have said identify.
-
- >If, for example, B is
-
- >class B
- >{
- >friend class A;
- >int fred;
- >}
-
- >I cannot just
-
- >A::A() {fred=21;};
-
- >or even
-
- >A::A() {B::fred=21;};
-
- >The As are only members of a class that has B as a base.
-
- >I suppose at its simplest level the problem is this
-
- >class B
- >{
- >int fred;
- > class A
- > {
- > A() { };
- > };
- >A a1;
- >A a2;
- >};
-
- >How within A::A do I refer to fred.
-
- You don't.
-
- Realize that, even though A is nested class of B, A and B are distinct
- types and not "linked" in the way you seen to think they are. In your
- sample code above, A has no more connection to or knowledge of B than
- some other class C in a totally different part of your program.
-
- If you need this kind of link, you must explicitly code it yourself.
- For example, you could arrange to pass, to an instance of A, a
- reference or pointer to an instance B, and access B::fred in *that*
- instance.
-
- A could have the following member...
-
- void A::access_fred(B* b)
- {
- b.fred = 42;
- }
-
-
-
- Regards,
- -------------------------------------------------------------------------
- Matt Arnold | | ||| | |||| | | | || ||
- marnold@netcom.com | | ||| | |||| | | | || ||
- Boston, MA | 0 | ||| | |||| | | | || ||
- 617.389.7384 (h) 617.576.2760 (w) | | ||| | |||| | | | || ||
- C++, MIDI, Win32/95 developer | | ||| 4 3 1 0 8 3 || ||
- -------------------------------------------------------------------------
-